1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use super::*;
use crate::settings::{load_optional, HasSettings};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct WindowSettings<T>
where
    T: HasSettings,
{
    pub shown: Option<bool>,
    pub position: Option<WindowPosition>,
    pub width: Option<f32>,
    pub height: Option<f32>,
    pub title_bar: Option<bool>,
    pub background: Option<bool>,
    pub title_bar_background: Option<bool>,
    pub resize: Option<bool>,
    pub auto_resize: Option<bool>,
    pub scroll: Option<bool>,
    pub scroll_bar: Option<bool>,
    pub hotkey: Option<WindowHotkey>,

    pub settings: Option<T::Settings>,
}

impl<T> WindowSettings<T>
where
    T: HasSettings,
{
    pub fn new(options: WindowOptions, settings: T::Settings) -> Self {
        let WindowOptions {
            visible,
            position,
            width,
            height,
            title_bar,
            background,
            title_bar_background,
            resize,
            auto_resize,
            scroll,
            scroll_bar,
            hotkey,
        } = options;
        Self {
            shown: Some(visible),
            position: Some(position),
            width: Some(width),
            height: Some(height),
            title_bar: Some(title_bar),
            background: Some(background),
            title_bar_background: Some(title_bar_background),
            resize: Some(resize),
            auto_resize: Some(auto_resize),
            scroll: Some(scroll),
            scroll_bar: Some(scroll_bar),
            hotkey: Some(hotkey),
            settings: Some(settings),
        }
    }
}

impl<T> Default for WindowSettings<T>
where
    T: HasSettings,
    T::Settings: Default,
{
    fn default() -> Self {
        Self::new(WindowOptions::default(), T::Settings::default())
    }
}

impl<T> HasSettings for Window<T>
where
    T: HasSettings,
{
    type Settings = WindowSettings<T>;

    const SETTINGS_ID: &'static str = T::SETTINGS_ID;

    fn current_settings(&self) -> Self::Settings {
        WindowSettings::new(self.options.clone(), self.inner.current_settings())
    }

    fn load_settings(&mut self, loaded: Self::Settings) {
        let Self::Settings {
            shown,
            position,
            width,
            height,
            title_bar,
            background,
            title_bar_background,
            resize,
            auto_resize,
            scroll,
            scroll_bar,
            hotkey,
            settings,
        } = loaded;

        load_optional(&mut self.options.visible, shown);
        load_optional(&mut self.options.position, position);
        load_optional(&mut self.options.width, width);
        load_optional(&mut self.options.height, height);
        load_optional(&mut self.options.title_bar, title_bar);
        load_optional(&mut self.options.background, background);
        load_optional(&mut self.options.title_bar_background, title_bar_background);
        load_optional(&mut self.options.resize, resize);
        load_optional(&mut self.options.auto_resize, auto_resize);
        load_optional(&mut self.options.scroll, scroll);
        load_optional(&mut self.options.scroll_bar, scroll_bar);
        load_optional(&mut self.options.hotkey, hotkey);

        if let Some(settings) = settings {
            self.inner.load_settings(settings);
        }
    }
}